<template>
    <div class="infoshow">
       <el-row type="flex" class="row-bg" justify="center">
           <el-col :span='8'>
               <div class="user">
                    <el-upload
                        class="avatar-uploader"
                        action="/api/avater/addImg/"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                        <img v-if="avatardata" :src="avatardata?avatardata:user.avatar" class="demo-avatar demo-basic avatar ">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                      </el-upload>
               </div>
             
           </el-col>
              <el-col :span='5'>
                    
                      </el-col>
           <el-col :span='16'>
               <div class="userinfo">
                  <div class="user-item">
                    <i class="fa fa-user"></i>
                   <span>{{user.name}}</span>
                  </div>
                  <div class="user-item">
                    <i class="fa fa-cog"></i>
                    <span>{{user.identity == 'manager' ? '管理员' : '普通员工'}}</span>
                  </div>
               </div>
           </el-col>
       </el-row>

 


    </div>
</template>
<script>
export default {
  name: 'infoshow',
  computed: {
    user() {
      return this.$store.getters.user
    },
  },

  data() {
    return {
      imageUrl: '',
      avatardata: sessionStorage.getItem('avatarUrl'),
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw)
      // console.log(file.response.newPath)
      sessionStorage.setItem('avatarUrl', file.response.newPath)

      // console.log(sessionStorage.getItem('avatarUrl'))

      let id = this.$store.getters.user.id
      location.reload()

      let params = {
        avatar: file.response.newPath,
      }
      // this.$axios.post(`/api/users/editavater/${id}`, params).then((res) => {
      //   this.$message({
      //     message: '保存成功！',
      //     type: 'success',
      //   })
      //   this.$emit('update')
      // })

      this.$axios({
        url: `/api/users/editavater/${id}`,
        method: 'post',
        data: {
          avatar: file.response.newPath,
        },
      }).then(function (response) {
        // console.log(response)
      })
    },

    // getavatar() {
    //   let id = this.$store.getters.user.id
    //   this.$axios({
    //     url: `/api/users/avatar/${id}`,
    //     method: 'get',
    //   }).then(function (response) {
    //     console.log('12312312312312312312')
    //     res = JSON.stringify(response)
    //     console.log(res)
    //   })
    // },

    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
  },
}
</script>
<style scoped>
.infoshow {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  /* padding: 16px; */
}
.row-bg {
  width: 100%;
  height: 100%;
}
.user {
  text-align: center;
  position: relative;
  top: 30%;
}
.user img {
  width: 150px;
  border-radius: 50%;
}
.user span {
  display: block;
  text-align: center;
  margin-top: 20px;
  font-size: 20px;
  font-weight: bold;
}
.userinfo {
  height: 100%;
  background-color: #eee;
}
.user-item {
  position: relative;
  top: 30%;
  padding: 26px;
  font-size: 28px;
  color: #333;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
